<template>
  <div class="app-container">
    <div class="filter-container" style="display: flex;align-items: center;justify-content: flex-start;flex-wrap:wrap">

      <div class="block">
        <el-date-picker v-model="value1" type="daterange" range-separator="至" size="small" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </div>

      <div>
        <span>姓名：</span>
        <el-input style="width:150px;margin-right:20px;" v-model="listQuery.name" size="small" placeholder="请输入姓名搜索"
          @keyup.enter.native="getList" clearable></el-input>
      </div>

      <div>
        <span>手机号：</span>
        <el-input style="width:150px;margin-right:20px;" v-model="listQuery.tel" size="small" placeholder="请输入手机号搜索"
          @keyup.enter.native="getList" clearable></el-input>
      </div>

      <div>
        <span>就业状态：</span>
        <el-select size="small" placeholder="请选择" clearable class="filter-item" style="width: 100px" v-model="listQuery.jobStatus">
          <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </div>

      <el-button size="small" class="filter-item" type="success" icon="el-icon-search" style="margin-left:3%" @click="handleFilter">
        搜索
      </el-button>
      <el-button size="small" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button size="small" class="filter-item" style="margin-left: 10px;" type="primary" @click="getout">导出Excel</el-button>

      <el-upload class="filter-item" action="#" v-loading="dialogLoading" :show-file-list="false" :http-request="uploadEx"
        accept=".xlsx,.xls">
        <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary">导入Excel</el-button>
      </el-upload>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" size="small" border fit highlight-current-row style="width: 100%;margin-top:1%">
      <el-table-column label="序号" type="index" width="50px">
      </el-table-column>
      <el-table-column label="姓名" min-width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="120px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.tel }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" width="180px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.idCard }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" width="50px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.age }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" width="50px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sex=='1'?'男':scope.row.sex=='0'?'女':'保密' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="就业状态" width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.jobStatus=='1'?'在岗':scope.row.jobStatus=='0'?'待业':'' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="就业单位" min-width="120px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.jobCompany}}</span>
        </template>
      </el-table-column>
      <el-table-column label="薪资水平" min-width="80px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.salary }}</span>

        </template>
      </el-table-column>
      <el-table-column label="工作地点" min-width="180px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.province }}{{ scope.row.city }}{{ scope.row.county }}</span>
        </template>
      </el-table-column>
      <el-table-column label="补充说明" min-width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.record }}</span>
        </template>
      </el-table-column>
      <el-table-column label="填报人" width="120px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.reportPerson }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button size="small" align="center" type="primary" @click="handleUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

    <!-- 弹出框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible"
      append-to-body>
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 700px; margin-left:50px;">
        <el-form-item label="姓名:" prop="name">
          <el-input v-model="temp.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号:" prop="tel">
          <el-input v-model="temp.tel" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="身份证号:" prop="idCard">
          <el-input v-model="temp.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="就业状态:" prop="jobStatus">
          <el-radio v-model="temp.jobStatus" label="1">在岗</el-radio>
          <el-radio v-model="temp.jobStatus" label="0">待业</el-radio>
        </el-form-item>
        <el-form-item label="就业单位:">
          <el-input v-model="temp.jobCompany" placeholder="请输入就业单位" />
        </el-form-item>
        <el-form-item label="薪资水平:">
          <el-input v-model="temp.salary" placeholder="请输入薪资水平" />
        </el-form-item>
        <el-form-item label="工作地点：">
          <v-distpicker :province="temp.province" :city="temp.city" :area="temp.county" @selected="select" />
        </el-form-item>

        <el-form-item label="填报人：" prop="reportPerson">
          <el-input v-model="temp.reportPerson" placeholder="请输入填报人手机" />
        </el-form-item>
        <el-form-item label="补充说明">
          <el-input type="textarea" :rows="4" placeholder="如果您还有其他诉求或说明,请在这里填写." v-model="temp.record">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          返回
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData('dataForm') : updateData('dataForm')">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // 异步请求 delLly
  import {
    getInfoList,
    uploadEx,
    exportEx,
    addUserInfo,
    putUserInfo
  } from '@/api/employment'
  import Vue from 'vue'
  import {
    parseTime
  } from '@/utils'
  import Pagination from '@/components/Pagination' // 分页插件
  import {
    SelectPage
  } from 'v-selectpage'

  const calendarTypeOptions = [{
      key: '0',
      display_name: '待业'
    },
    {
      key: '1',
      display_name: '在岗'
    }
  ]

  const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
    acc[cur.key] = cur.display_name
    return acc
  }, {})


  export default {
    inject: ["reload"],
    components: {
      Pagination,
      'v-selectpage': SelectPage
    },

    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger'
        }
        return statusMap[status]
      },
      typeFilter(type) {
        return calendarTypeKeyValue[type]
      },
      channelTypeFilter(type) {
        return channelTypeKeyValue[type]
      }
    },
    data() {
      return {
        dialogLoading: false,
        value1: '',
        sample: [],
        profession: [],
        deptValue: '',
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          pageNum: 1,
          pageSize: 10,
          sort: '+id',
          jobStatus: '',
          name: '',
          tel: ''
        },
        deps: [],
        sortOptions: [{
          label: 'ID Ascending',
          key: '+id'
        }, {
          label: 'ID Descending',
          key: '-id'
        }],
        statusOptions: ['published', 'draft', 'deleted'],
        showReviewer: false,
        temp: {
          id: undefined
        },
        dialogFormVisible: false,
        calendarTypeOptions,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '添加',
          examine: '查看详情'
        },
        rowId: "",
        dialogPvVisible: false,
        outerVisible: false,
        pvData: [],
        rules: {
          name: [{
            required: true,
            message: '姓名不能为空',
            trigger: 'blur'
          }],
          tel: [{
            required: true,
            message: '手机号不能为空',
            trigger: 'blur'
          }],
          idCard: [{
            required: true,
            message: '身份证号不能为空',
            trigger: 'blur'
          }],
          reportPerson: [{
            required: true,
            message: '填报人不能为空',
            trigger: 'blur'
          }],
          jobStatus: [{
            required: true,
            message: '请选择就业状态',
            trigger: 'change'
          }]
        },
        downloadLoading: false,
        liaUser: null,
        ids: [],
        // select: '',
        allInfo: []
      }
    },
    created() {
      this.getList()
    },
    methods: {
      // 获取list
      getList() {
        this.listLoading = true
        var params = {
          pageNum: this.listQuery.pageNum - 1,
          pageSize: this.listQuery.pageSize,
          jobStatus: this.listQuery.jobStatus,
          startTime: this.value1 ? this.value1[0] : '',
          endTime: this.value1 ? this.value1[1] : '',
          name: this.listQuery.name,
          tel: this.listQuery.tel
        }
        console.log(this.listQuery)
        getInfoList(params).then(response => {
          this.list = response.data.content
          console.log(this.list)
          this.total = response.data.totalElements
          this.listLoading = false
        })
      },
      // 搜索
      handleFilter() {
        this.listQuery.page = 1
        this.listQuery.pageNum = 1,
          this.getList()
      },


      fabuDate() {
        console.log(this.radio)
        if (this.radio === 1) {
          this.temp.pubTime = ''
        } else if (this.radio === 2) {
          return this.temp.pubTime
        }
      },


      handleModifyStatus(row, status) {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        row.status = status
      },
      sortChange(data) {
        const {
          prop,
          order
        } = data
        if (prop === 'id') {
          this.sortByID(order)
        }
      },
      sortByID(order) {
        if (order === 'ascending') {
          this.listQuery.sort = '+id'
        } else {
          this.listQuery.sort = '-id'
        }
        this.handleFilter()
      },
      resetTemp() {
        this.temp = {
          id: undefined
        }
      },

      // 编辑
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      // 修改数据
      updateData() {
        const tempData = Object.assign({}, this.temp)
        console.log(tempData)
        putUserInfo(tempData).then(() => {
          this.dialogFormVisible = false
          this.$notify({
            title: '成功',
            message: '修改成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      },

      // 地址选择
      select(data) {
        console.log('data', data)
        this.temp.provinceCode = data.province.code + '000000'
        this.temp.cityCode = data.city.code + '000000'
        this.temp.countyCode = data.area.code + '000000'
        this.temp.province = data.province.value
        this.temp.city = data.city.value
        this.temp.county = data.area.value
      },

      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData(form) {
        console.log(this.temp)
        this.$refs[form].validate((isPass)=>{
          if(isPass){
          addUserInfo(this.temp).then((res) => {
          
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
          }else{
           this.$notify({
             title: '失败',
             message: '信息填写不完整',
             type: 'danger',
             duration: 2000
           })
          }
        })


      },
      getSortClass: function(key) {
        const sort = this.listQuery.sort
        return sort === `+${key}` ?
          'ascending' :
          sort === `-${key}` ?
          'descending' :
          ''
      },

      getout() {
        exportEx({}).then(res => {
          console.log(res)
          let url = window.URL.createObjectURL(new Blob([res]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', '信息采集.xlsx')

          document.body.appendChild(link)
          link.click()
          // let blob = new Blob([res], {type: res.type})
          //  let downloadElement = document.createElement('a')
          //  let href = window.URL.createObjectURL(blob); //创建下载的链接
          //  downloadElement.href = href;
          //  downloadElement.download = fileName; //下载后文件名
          //  document.body.appendChild(downloadElement);
          //  downloadElement.click(); //点击下载
          //  document.body.removeChild(downloadElement); //下载完成移除元素
          //  window.URL.revokeObjectURL(href); //释放blob对象
        })
      },
      uploadEx(data) {
        console.log(data)
        this.dialogLoading = true;
        const formData = new FormData();
        formData.append("file", data.file);
        uploadEx(formData).then(res => {
          console.log(res)
          this.dialogLoading = false;
          if (res.code == '200') {
            this.$notify({
              title: '成功',
              message: '成功添加' + res.data + '条数据',
              type: 'success'
            })
          }
          this.getList()
        }).catch((res) => {
          this.dialogLoading = false;
          console.log(res)
        });
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
